import {inject} from "vue"
import { createRouter, createWebHistory,createWebHashHistory } from 'vue-router'
import { useUserStore } from '../stores/user'
import { useLicenseStore } from '../stores/license'
import IndexView from '../views/Index.vue'
import LoginView from '../views/Login.vue' 
 
const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'home',
    meta: {no_login: true},
    component: IndexView
  },
  {
    path: '/login',
    name: 'login',
    meta: {no_login: true},
    component: LoginView
  }
]

const router = createRouter({
  // history: createWebHistory(process.env.BASE_URL),
  history:createWebHashHistory(),
  routes
})
//全局前置守卫
router.beforeEach((to, from, next) => {
  // showLoadingToast({message: t('Loading'), forbidClick: true, duration: 0})
  const userStore = useUserStore()
  const licenseStore = useLicenseStore()   
  //强制认证 
  // if(to.name=='theme'&&userStore.auth){
  //   to.meta.no_login=false;
  // }
  if (to.meta.no_login) { // 无需登录的页面直接进入
    next()
  } else if (JSON.stringify(to.meta)!="{}"&&(!userStore.orgId||!userStore.userinfo)&&licenseStore.auth) { // 未登录或登录失效 
    localStorage.setItem("lastPath",from.fullPath)
    next({ path: '/login', replace: true })
  } else { // 已经登陆
    next()
  }
})

router.afterEach(() => {
  // closeToast()
})

export default router
